package com.atguigu.service.impl;

import com.atguigu.entity.EduCourse;
import com.atguigu.entity.EduCourseDescription;
import com.atguigu.entity.frontvo.CourseFrontVo;
import com.atguigu.entity.frontvo.CourseWebVo;
import com.atguigu.entity.vo.CourseInfoVo;
import com.atguigu.entity.vo.CoursePublishVo;
import com.atguigu.exceptionhandler.GuliException;
import com.atguigu.mapper.EduCourseMapper;
import com.atguigu.service.EduChapterService;
import com.atguigu.service.EduCourseDescriptionService;
import com.atguigu.service.EduCourseService;
import com.atguigu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-04
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述的注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //小节和章节的service
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo
     **/
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1、向课程表添加课程基本信息
        //CourseInfoVo转换为EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0){
            //添加失败
            throw new GuliException(20001, "添加课程信息失败");
        }

        //2、向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        //将课程的id赋给描述的id
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(eduCourseDescription);
        //返回课程id
        return eduCourse.getId();
    }

    /**
     * 根据课程id查询课程的基本信息
     *
     * @param courseId
     **/
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1、先查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //2、查询课程简介表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);

        //创建一个返回的课程基本信息实体类
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());


        return courseInfoVo;
    }

    /**
     * 修改课程基本信息
     *
     * @param courseInfoVo
     **/
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        //修改课程表
        int i = baseMapper.updateById(eduCourse);

        //修改简介表
        EduCourseDescription courseDescription = new EduCourseDescription();

        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());

        boolean b = courseDescriptionService.updateById(courseDescription);

        if(i <= 0 || !b){
            throw new GuliException(20001, "修改课程信息失败");
        }
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param id
     **/
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }

    /**
     * 删除课程
     *
     * @param courseId
     **/
    @Override
    public void removeCourse(String courseId) {
        //1、根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);

        //2、根据课程id删除章节
        chapterService.removeChapterBYCourseId(courseId);

        //3、根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4、根据课程id删除课程本身
        int rows = baseMapper.deleteById(courseId);
        if(rows == 0 ){
            throw new  GuliException(20001,"'删除课程失败'");
        }
    }

    /**
     * 条件查询带分页查询课程
     *
     * @param pageCourse
     * @param courseFrontVo
     **/
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //判断条件值是否为空
        //以及分类
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            System.out.println("-----" + courseFrontVo.getSubjectParentId());
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }

        //二级分类
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }

        //关注度/销量 排序
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }

        //最新 排序
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }

        //价格 排序
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse, wrapper);

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();    //是否有下一页
        boolean hasPrevious = pageCourse.hasPrevious();    //是否有上一页

        //分页数据放到map集合中
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    /**
     * 根据课程id，编写sql语句查询课程信息
     *
     * @param courseId
     **/
    @Override
    public CourseWebVo getBaseCpirseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
